#include<bits/stdc++.h>
using namespace std;
const int N=200010,N1=2005;
int n;
int x;
int T=0;
int a[N];
int f[N];
int ans;
int cnt=1;
bool b[N];
void dfs(int deep){
    if(deep>n){
        cnt=0;
        for(int i=2;i<=n;i++){
            for(int j=i-1;j>=1;j--){
                if(b[i]==b[j]){
                    if(a[i]==a[j]){
                        cnt+=a[j];
                    }
                    break;
                }
            }
        }
        ans=max(ans,cnt);
        return ;
    }
    b[deep]=true;
    dfs(deep+1);
    b[deep]=false;
    dfs(deep+1);
    return;
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&T);
    //T=2;
    while(T--){
        ans=0;
        cnt=1;
        scanf("%d",&n);
        if(n<=16){
            ans=0;
            //scanf("%d",&n);
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            dfs(1);
            printf("%d\n",ans);
            continue;
        }
        memset(a,0,sizeof(a));
        memset(f,0,sizeof(f));
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            if(x==a[cnt-1]){
                ans+=a[cnt-1];
            }
            else{
                a[cnt]=x;
                cnt++;
            }
        }
        cnt--;
        n=cnt;
        for(int i=2;i<=n;i++){
            for(int j=i-1;j>=1;j--){
                if(a[i]==a[j]){
                    if(a[i+1]==a[j-1]&&a[i+1]!=0){
                        x=a[i+1];
                    }
                    else{
                        x=0;
                    }
                    f[i]=max(f[i],f[j]+a[i]+x);

                }
                else{
                    if(a[i+1]==a[j-1]&&a[i+1]!=0){
                        x=a[i+1];
                    }
                    else{
                        x=0;
                    }
                    f[i]=max(f[i],f[j]+x);
                }
            }
        }
        printf("%d\n",ans+f[n]);
    }
    return 0;
}
